const mongoose = require('mongoose')

const MentorshipSchema = new mongoose.Schema(
  {
    classId: {
      type: String,
      required: true,
      default: function () {
        return this._id.toString()
      }
    },
    // 是否成功案例
    isSuccessCase: {
      type: Number,
      required: true,
      default: 0
    },
    startDate: {
      type: Date,
      required: true,
      get: v => v.toISOString().split('T')[0] // 格式化输出为YYYY-MM-DD
    },

    files: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'File',
      required: true
    },
    className: {
      type: String,
      required: true,
      maxlength: 50
    },
    description: {
      type: String,
      required: true,
      maxlength: 500
    },
    createdAt: {
      type: Date,
      default: Date.now
    },
    year: {
      type: Number,
      required: true,
      default: function () {
        const dateObj = this.startDate instanceof Date ? this.startDate : new Date(this.startDate)
        return dateObj.getUTCFullYear()
      }
    }
  },
  { timestamps: true }
)

MentorshipSchema.set('toJSON', { getters: true })
MentorshipSchema.index({ classId: 1, year: 1, isSuccessCase: 1 })

module.exports = mongoose.model('Mentorship', MentorshipSchema)
